<?php

class Validata{

    static function validataUser(User $user){
        //print_r($user);
        $username = $user->getUserName();
        $password = $user->getPassWord();
        unset($_SESSION['validata_username'],$_SESSION['validata_password']);

        try{
            self::validataUserName($username);
        }catch(MyException $e){
            $_SESSION['validata_username'] = $e->getMessage();
        }
        try{
            self::validataPassword($password);
        }catch(MyException $e){
            $_SESSION['validata_password'] = $e->getMessage();
        }

        if(isset($e)){
            throw $e;
        }
    }
    static private function validataUserName($username){
        $len = strlen($username);
        if($len<3){
            //echo '用户名长度不能小于3';
            throw new MyException('用户名长度不能小于3',E_USER_ERROR);
        }
        if($len>8){
            //echo '用户名长度不能小于3';
            throw new MyException('用户名长度不能大于8',E_USER_ERROR);
        }
    }

    static private function validataPassword($password){
        $len = strlen($password);
        if($len<3){
            //echo '密码名长度不能小于3';
            throw new MyException('密码长度不能小于3',E_USER_ERROR);
        }
        if($len>8){
            //echo '密码长度不能小于3';
            throw new MyException('密码长度不能大于8',E_USER_ERROR);
        }
    }
}